package sunwin.yoms.service.security;

import jodd.datetime.JDateTime;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import sunwin.yoms.comm.IPAddressHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.domain.system.AdminLoginLogDomain;
import sunwin.yoms.domain.system.FireWallDomain;
import sunwin.yoms.service.system.AdminLoginLogService;
import sunwin.yoms.service.system.FireWallService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by mt on 2015-10-30.
 */
public class YomsAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Autowired
    private YomsServiceDefaultData yomsControllerDefaultData;
    @Autowired
    private AdminLoginLogService adminLoginLogService;
    @Autowired
    private IPAddressHelper ipAddressHelper;
    @Autowired
    private FireWallService fireWallService;


    private final String login = "login";
    private final String index = "default";
    private final String error403 = "error/403";

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //获取当前用户信息
        YomsUserDetails userDetails = yomsControllerDefaultData.getCurrentYomsUserDetails();
        //当前登陆名
        String loginName = userDetails.getUsername();
        AdminLoginLogDomain adminLoginLogDomain = new AdminLoginLogDomain();
        adminLoginLogDomain.setLoginName(loginName);
        adminLoginLogDomain.setLoginPass(2);
        adminLoginLogDomain.setLoginIp(ipAddressHelper.getIpAddr(request));
        adminLoginLogDomain.setLoginTime(new JDateTime(System.currentTimeMillis()).convertToDate());
        try {
            adminLoginLogService.addAdminLoginLog(adminLoginLogDomain);
        } catch (YomsException e) {
            e.printStackTrace();
        }

        //2、获取客户端IP
        String clientIp = IPAddressHelper.getIpAddr(request);
        Logger.getLogger(this.getClass()).info("访问后台登录页面IP地址：" + clientIp);
        //1、获取数据库防火墙列表里面的IP
        FireWallDomain fireWallDomain = new FireWallDomain();
        List<FireWallDomain> domains = null;
        try {
            domains = fireWallService.getFireList(fireWallDomain);
        } catch (YomsException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < domains.size(); i++) {
            FireWallDomain domain = domains.get(i);
            if (domain.getFirePass() == 1) {
                if (clientIp.equals(domain.getFireIp())) {
                    response.sendRedirect("/error/403.html");
                } else {
                    response.sendRedirect("/error/403.html");
                }
            }
        }
        for (int i = 0; i < domains.size(); i++) {
            if (domains.get(i).getFirePass() == 2) {
                if (clientIp.equals(domains.get(i).getFireIp())) {
                    response.sendRedirect("/default.html");
                }
            }
        }
    }
}
